package com.mingxun.project;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.mingxun.project.base.BaseActivity;
import com.mingxun.project.ui.main.fragment.FourFragment;
import com.mingxun.project.ui.main.fragment.MainFragment;
import com.mingxun.project.ui.main.fragment.ThreeFragment;
import com.mingxun.project.ui.main.fragment.TwoFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;
import com.roughike.bottombar.TabSelectionInterceptor;

public class MainActivity extends BaseActivity {
    private MainFragment mMainFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private FragmentTransaction transaction;
    private BottomBar bottomBar;
    private BottomBarTab nearby;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                selectTabMenu(tabId);
            }
        });

        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(@IdRes int tabId) {
                if (tabId == R.id.tab_nearby) {
                    // 已经选择了这个标签，又点击一次。即重选。
                    nearby.removeBadge();
                }
            }
        });

        bottomBar.setTabSelectionInterceptor(new TabSelectionInterceptor() {
            @Override
            public boolean shouldInterceptTabSelection(@IdRes int oldTabId, @IdRes int newTabId) {
                // 点击无效
                if (newTabId == R.id.tab_restaurants) {
                    // 返回 true 。代表：这里我处理了，你不用管了。
                    return true;
                }

                return false;
            }
        });
    }

    @Override
    protected void onInitData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void initPresenter() {

    }

    //选择底部菜单
    public void selectTabMenu(int tabId) {
        switch (tabId) {
            case R.id.tab_recents:
                if (null == mMainFragment) {
                    mMainFragment = MainFragment.newInstance();
                }
                replaceFragment(mMainFragment);
                break;
            case R.id.tab_favorites:
                if (null == twoFragment) {
                    twoFragment = TwoFragment.newInstance();
                }
                replaceFragment(twoFragment);
                break;
            case R.id.tab_nearby:
                nearby = bottomBar.getTabWithId(R.id.tab_nearby);
                nearby.setBadgeCount(5);
                if (null == threeFragment) {
                    threeFragment = ThreeFragment.newInstance();
                }
                replaceFragment(threeFragment);
                break;
            case R.id.tab_friends:
                if (null == fourFragment) {
                    fourFragment = FourFragment.newInstance();
                }
                replaceFragment(fourFragment);
                break;
            case R.id.tab_restaurants:
                break;
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        transaction = fm.beginTransaction();
        transaction.replace(R.id.main_fragment_layout, fragment);
        transaction.commit();

    }
}
